package com.ramotion.expandingcollection;

import ohos.agp.components.AttrSet;
import ohos.agp.components.PageSlider;
import ohos.app.Context;

import java.util.List;

/**
 * EcPageSilder
 *
 * @since 2021-06-21
 */
public class EcPager extends PageSlider {
    /**
     * PAGE_MARGIN
     */
    public static final int PAGE_MARGIN = -390;

    private static final int CONSTANT_3 = 3;
    private static final int CONSTANT_650 = 650;
    private static final int CONSTANT_700 = 700;
    private EcBackgroundSwitcherView attachedImageSwitcher;
    private int oldPosition;
    private Integer cardWidth;
    private Integer cardHeight;
    private OnCardSelectedListener onCardSelectedListener;
    private List<EcCardData> dataSet;
    private boolean isSwitch;

    /**
     * EcPageSlider
     *
     * @param context
     * @param attrSet
     */
    public EcPager(Context context, AttrSet attrSet) {
        this(context, attrSet, null);
    }

    /**
     * EcPageSlider
     *
     * @param context
     * @param attrSet
     * @param styleName
     */
    public EcPager(Context context, AttrSet attrSet, String styleName) {
        super(context, attrSet, styleName);
        initializeFromAttributes(attrSet);
        init();
    }

    /**
     * initializeFromAttributes
     *
     * @param attrs
     */
    protected void initializeFromAttributes(AttrSet attrs) {
        this.cardWidth = AttrUtils.getDimensionFromAttr(attrs, "cardWidth", CONSTANT_700);
        this.cardHeight = AttrUtils.getDimensionFromAttr(attrs, "cardHeight", CONSTANT_650);
    }

    /**
     * isIsSwitch
     *
     * @return true
     */
    public boolean isSwitch() {
        return isSwitch;
    }

    /**
     * init
     */
    private void init() {
        setPageCacheSize(CONSTANT_3);
        setPageMargin(PAGE_MARGIN);
        addPageChangedListener(new PageChangedListener() {
            @Override
            public void onPageSliding(int position, float positionOffset, int positionOffsetPixels) {
                oldPosition = getCurrentPage();
                isSwitch = false;
            }

            @Override
            public void onPageSlideStateChanged(int state) {
                isSwitch = true;
            }

            @Override
            public void onPageChosen(int position) {
                EcBackgroundSwitcherView.AnimationDirection direction = null;
                if (oldPosition < position) {
                    direction = EcBackgroundSwitcherView.AnimationDirection.LEFT;
                } else if (oldPosition > position) {
                    direction = EcBackgroundSwitcherView.AnimationDirection.RIGHT;
                }
                if (attachedImageSwitcher != null) {
                    attachedImageSwitcher.updateCurrentBackgroundAsync(EcPager.this, direction);
                }
                if (onCardSelectedListener != null) {
                    onCardSelectedListener.cardSelected(position, oldPosition, getProvider().getCount());
                }
            }
        });
    }

    /**
     * getCardWidth
     *
     * @return cardWidth
     */
    public int getCardWidth() {
        return cardWidth;
    }

    /**
     * getCardHeight
     *
     * @return cardHeight
     */
    public int getCardHeight() {
        return cardHeight;
    }

    /**
     * setPagerViewAdapter
     *
     * @param provider
     * @param list
     */
    public void setPagerViewAdapter(EcPagerViewAdapter provider, List<EcCardData> list) {
        this.dataSet = list;
        setProvider(provider);
    }

    /**
     * Set {@link OnCardSelectedListener} to pager view.
     *
     * @param listener
     */
    public void setOnCardSelectedListener(OnCardSelectedListener listener) {
        this.onCardSelectedListener = listener;
        if (listener == null) {
            return;
        }
        if (getProvider() != null) {
            onCardSelectedListener.cardSelected(getCurrentPage(), getCurrentPage(), getProvider().getCount());
        }
    }

    /**
     * Attach {@link EcBackgroundSwitcherView} element to pager view
     *
     * @param imageSwitcher already inflated {@link EcBackgroundSwitcherView} element
     */
    public void setBackgroundSwitcherView(EcBackgroundSwitcherView imageSwitcher) {
        this.attachedImageSwitcher = imageSwitcher;
        if (imageSwitcher == null) {
            return;
        }
        if (getProvider() != null) {
            attachedImageSwitcher.setDataSet(dataSet);
            attachedImageSwitcher.updateCurrentBackground(this, null);
        }
    }

    /**
     * OnCardSelectedListener
     *
     * @since 2021-06-21
     */
    public interface OnCardSelectedListener {
        /**
         * cardSelected
         *
         * @param newPosition
         * @param oldPosition
         * @param totalElements
         */
        void cardSelected(int newPosition, int oldPosition, int totalElements);
    }
}
